<?php

namespace App\Http\Controllers\Strator;

use App\Http\Controllers\Controller;
use App\Models\Strator\StratorProductAttr;
use App\Services\Strator\StratorProductAttrService;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class StratorProductAttrController extends Controller
{
    protected Request $request;
    protected Model $model;

    public function __construct(Request $request,StratorProductAttr $stratorProductAttr)
    {
        $this->request=$request;
        $this->model = $stratorProductAttr;
    }

    //
    public function allInTree(StratorProductAttrService $ProductAttrService){
        $tree=$ProductAttrService->getStratorProductAttrs();
        return $this->success($tree);
    }

    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $perPage=$this->request->per_page??10;
        $list=$this->model->modelSearch($this->request,$this->model)
            ->with(['parent'])
            ->orderByDesc('id')
            ->paginate($perPage);
        return $this->success($list);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store()
    {
        $data=$this->request->only($this->storeOnly());
        $this->model->fill($data);
        $this->model->save();
        $this->saveOperationLog('添加产品属性');
        return $this->success(['id'=>$this->model->id]);
    }
    //
    public function storeOnly():array{
        return [
            'pid',
            'name',
            'type_code',
            'type_remark',
            'one_name',
            'two_name',
            'remark',
            'sort',
            'diy_attr',
        ];
    }

    /**
     * Display the specified resource.
     */
    public function show()
    {
        $model=$this->model->findOrFail($this->request->id);
        return $this->success($model);
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(StratorProductAttr $stratorProductAttr)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update()
    {
        $detail=$this->model->findOrFail($this->request->id);
        $data=$this->request->except($this->updateExcept());
        $detail->fill($data);
        $detail->save();
        // 如果修改的是一级属性，要同步更新全部下属的one_name
        if($detail->pid == 0 && $this->request->id){
            $this->model->where('pid', $detail->id)->update(['one_name'=>$this->request->name,'two_name'=>null]);
            $childdata = $this->model->where('pid', $detail->id)->get()->toArray();
            if(count($childdata)){
                foreach ($childdata as $item){
                    $this->model->where('pid', $item['id'])->update(['one_name'=>$this->request->name,'two_name'=>$item['name']]);
                }
            }
        }
        $this->saveOperationLog('修改产品属性');
        return $this->successMsg();
    }
    //
    public function updateExcept():array
    {
        return [
            'id',
        ];
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy()
    {
        $model=$this->model->findOrFail($this->request->id);
        $model->delete();
        $this->saveOperationLog('删除产品属性');
        return $this->successMsg('删除成功');
    }


}
